/**
 * Created by Administrator on 2016/7/1.
 */
"use strict";
swan_pc
    .controller("DetailShowController",function($scope,TimeExchange,TimeCheck,$state,Clip,$rootScope,$timeout){
        Clip.get({clipid:$rootScope.presentEventid},function(result){
            $scope.presentClip=result;
        })
        $scope.init_show=function(){
            $scope.InfoShow=true;
        }
        $scope.init_show();
        $scope.display=function(option){
            if(option==0){
                $scope.InfoShow=false;
            }
            if(option==1){
                $scope.InfoShow=true;
            }
        }
        $scope.addclip={
            "actionRemark": null,
            "address": null,
            "advanceInfo": null,
            "category": null,
            "checkList": null,
            "code": null,
            "color": null,
            "deletedAt": null,
            "endTime": null,
            "individualTaskId": null,
            "isDelayed": null,
            "isFixed": null,
            "isImportant": null,
            "isPrincipal": null,
            "isSecret": null,
            "level": null,
            "linkedActivityId": null,
            "linkedActivityType": null,
            "name": null,
            "organizationId": null,
            "ownerId": null,
            "previewTime": null,
            "relatedCompany": null,
            "relatedContact":null,
            "relatedOpp": null,
            "remark": null,
            "remind": null,
            "startTime":null,
            "status":null,
            "type": null,
            "who": null
        }
        $scope.DeleteToDatabase=function(){
            swal({
                title: "确定要删除该日程吗",
                text: "删除后将无法恢复！！",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认删除",
                cancelButtonText: "取消删除",
                closeOnConfirm: false,
                closeOnCancel: false
            }, function(isConfirm) {
                if (isConfirm) {
                    Clip.delete({clipid:$rootScope.presentEventid},
                        function(result){
                            swal("删除成功",$scope.presentClip.name,"success");
                            $state.go("calendarshow");
                        },
                        function(result){
                            swal("删除失败",$scope.presentClip.name,"error");
                        })
                }else
                {     swal("删除取消",$scope.presentClip.name,"error");
                }
            });
        };
        $scope.BackToCal=function(){
            $rootScope.goSref("agendalistshow");
        }
        $scope.ClipKey={
            "name":"日程内容",
            "startTime":"开始时间",
            "endTime":"结束时间",
            "address":"日程地点",
            "level":"层次",
            "actionRemark":"行动备注" ,
            "advanceInfo": "高级信息",
            "category" : "公事/私事",
            "checkList" : "物品检查" ,
            "code": "编号",
            "color" : "颜色" ,
            "createdBy":null,
            "createdDate": null,
            "deletedAt" : "删除时间" ,
            "id" : "ID" ,
            "individualTaskId": "任务ID" ,
            "isDelayed":"是否拖延",
            "isFixed": "是否固定时间",
            "isImportant": "是否重要" ,
            "isPrincipal" : "是否属于组织" ,
            "isSecret": "是否私密",
            "lastModifiedBy":null,
            "lastModifiedDate":null,
            "linkedActivityId": "link活动Id" ,
            "linkedActivityType":"link活动类型",
            "organizationId" : "组织ID" ,
            "ownerId" : "UserID" ,
            "previewTime" : "预计时间",
            "relatedCompany": "公司",
            "relatedContact":"相关联系人",
            "relatedOpp": "机会",
            "remark": "备注" ,
            "remind": "提醒时间" ,
            "status":"状态",
            "type": "类型" ,
            "who" : "行动对象"
        }
        $scope.show=function(index){
            var x=document.getElementById("show"+index);
            //alert(x.value);
            $timeout(function(){
                x.focus();
            });
        }
        $scope.TimeShow=function(time){
            if(time==null){
                return "";
            }
            var timer=time.toString();
            timer=TimeExchange.StringtoLocalTime(timer);
            //console.log("clipdetail"+time + " " + timer);
            return timer;
        };
        $scope.EditToDatabase=function(){
            console.log("$scope.start:"+$scope.presentClip.startTime);
            console.log("$scope.end:"+$scope.presentClip.endTime);
            //console.log("$scope.name:"+$scope.presentClip.name);
            var flag=TimeCheck.timecheck($scope.presentClip.startTime,$scope.presentClip.endTime);
            if($scope.presentClip.name==null || $scope.presentClip.name==""){
                swal("日程内容不能为空",$scope.presentClip.name,"error");
            }
            if(flag==0){
                swal("日程不能跨天",$scope.presentClip.name,"error");
            }
            if(flag==1){
                swal("日程不能短于30分钟",$scope.presentClip.name,"error");
            }
            if(flag==3){
                swal("结束时间不得早于开始时间",$scope.presentClip.name,"error");
            }
            if(flag==2 &&($scope.presentClip.name!=null)&&($scope.presentClip.name!="")){
                Clip.update({}, $scope.presentClip,
                    function (result) {
                        $rootScope.calendarDirty = false;
                        swal("更新成功", "日程" + " " + $scope.presentClip.name, "success");
                        $state.go("agendalistshow");
                    },
                    function (result) {
                        swal("更新失败", "日程" + " " + $scope.presentClip.name, "error");
                    })
            }
        }
        })
